package com.fengwei.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author JamesLee
 */
public class ResponseUtil {
    private final static Logger logger = LoggerFactory.getLogger(ResponseUtil.class);

    public static Map<String, Object> toJson(Enum resultStatus, Object data) {
        Class clazz = resultStatus.getDeclaringClass();
        Map<String, Object> returnParam = new HashMap<String, Object>();
        try {
            Method toCode = clazz.getMethod("getpCode");
            Method toName = clazz.getMethod("getpName");
            returnParam.put(PltResult.PLT_RESULT, toCode.invoke(resultStatus));
            returnParam.put(PltResult.PLT_RESULTDESC, toName.invoke(resultStatus));
            if (null == data) {
                returnParam.put("data", new HashMap<>(16));
            } else {
                returnParam.put("data", data);
            }
        } catch (Exception e) {
            logger.error("Error", e);
        }
        return returnParam;
    }

    public static Map<String, Object> toJsonWithMsgToken(Enum resultStatus, Object data, String msgtoken) {
        Class clazz = resultStatus.getDeclaringClass();
        Map<String, Object> returnParam = new HashMap<String, Object>();
        try {
            Method toCode = clazz.getMethod("getpCode");
            Method toName = clazz.getMethod("getpName");
            returnParam.put(PltResult.PLT_RESULT, toCode.invoke(resultStatus));
            returnParam.put(PltResult.PLT_RESULTDESC, toName.invoke(resultStatus));
            if (null == data) {
                returnParam.put("data", new HashMap<>(16));
            } else {
                returnParam.put("data", data);
            }
            returnParam.put("msgtoken", msgtoken);
        } catch (Exception e) {
            logger.error("Error", e);
        }
        return returnParam;
    }

    public static Map<String, Object> toJsonWithErrorDesc(Enum resultStatus, String resultDesc, Map<String, Object> data) {
        Class clazz = resultStatus.getDeclaringClass();
        Map<String, Object> returnParam = new HashMap<String, Object>();
        try {
            Method toCode = clazz.getMethod("getpCode");
            returnParam.put(PltResult.PLT_RESULT, toCode.invoke(resultStatus));
            returnParam.put(PltResult.PLT_RESULTDESC, resultDesc);
            if (null == data) {
                returnParam.put("data", new HashMap<>(16));
            } else {
                returnParam.put("data", data);
            }
        } catch (Exception e) {
            logger.error("Error", e);
        }
        return returnParam;
    }

    public static Map<String, Object> toJson(String errCd, String errDsp, Object data) {
        Map<String, Object> returnParam = new HashMap<String, Object>();
        try {
            returnParam.put(PltResult.PLT_RESULT, errCd);
            returnParam.put(PltResult.PLT_RESULTDESC, errDsp);
            if (null == data) {
                returnParam.put("data", new HashMap<>(16));
            } else {
                returnParam.put("data", data);
            }
        } catch (Exception e) {
            logger.error("Error", e);
        }
        return returnParam;
    }
}
